<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>求购信息</title>
    <link href="../plugin/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../plugin/jquery/jquery-3.5.1.min.js"></script>
    <script type="text/javascript" src="../plugin/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <script type="text/javascript" src="../plugin/layer/layer.js"></script>
    <script type="text/javascript" src="../base.js"></script>
    <style>
        button {
            margin-left: 10px;
        }

        .form {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
        }
    </style>
</head>
<body>
<form style="margin: 20px 0 20px 100px" class="form-inline">
    <div class="form-group">
        <label for="keyword">关键词</label>
        <input type="text" class="form-control" id="keyword"/>
    </div>
    <div class="form-group">
        <label for="galaxy">星系</label>
        <input type="text" class="form-control" id="galaxy"/>
    </div>
    <div class="form-group">
        <label for="nickName">发布人</label>
        <input type="text" class="form-control" id="nickName"/>
    </div>
    <button type="button" class="btn btn-primary" onclick="getList()">查询</button>
    <button type="button" class="btn btn-primary" onclick="publish()">我也要发布</button>
</form>

<div class="form" style="margin: 10px 100px 0 100px; text-align: center;">
    <table class="table" id="list">
        <tr>
            <td>标题</td>
            <td>内容</td>
            <td>所在星系</td>
            <td>发布人</td>
            <td>时间</td>
            <td>操作</td>
        </tr>
    </table>
</div>
</body>
<script>
    let type = 1;
    $(function(){
        getList();
    });

    //获取列表
    function getList() {
        $.ajax({
            url: domain + '/market/list',
            type: 'get',
            dataType: 'json',
            data: {
                'keyword': $('#keyword').val(),
                'galaxy': $('#galaxy').val(),
                'nickName' : $('#nickName').val(),
                'type': type
            },
            success: function (data) {
                if (data.code === 0) {
                    let list = data.data;
                    $("#list").html('<tr><td>标题</td><td>内容</td><td>所在星系</td><td>发布人</td><td>时间</td><td>操作</td></tr>');
                    for (let i = 0; i < list.length; i++) {
                        let item = list[i];
                        $("#list").append('<tr>' +
                            '<td>' + item.title + '</td>' +
                            '<td>' + item.content.substring(0, 10) + '...</td>' +
                            '<td>' + item.galaxy + '</td>' +
                            '<td>' + item.nickName + '</td>' +
                            '<td>' + item.date + '</td>' +
                            '<td><button onclick="showDetail(\'' + item + '\')">详情</button><button onclick="deleteItem(\'' + item.id + '\')">删除</button></td>' +
                            '</tr>');
                    }
                } else {
                    layer.msg(data.msg);
                }
            }
        })
    }

    //详情
    function showDetail(id) {
        $.ajax({
            url: domain + '/market/detail',
            type: 'get',
            dataType: 'json',
            data: {
                'id': id,
                'type': type
            },
            success: function (data) {
                if (data.code === 0) {
                    let result = data.data;
                    let content = '标题:' + result.title + '</br>';
                    content += '内容:' + result.content + '</br>';
                    content += '时间:' + result.date + '</br>';
                    content += '发布人:' + result.nickName + '</br>';
                    content += '游戏编号:' + result.code + '</br>';
                    content += '所在星系:' + result.galaxy + '</br>';
                    layer.alert(content);
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    }
    
    //删除
    function deleteItem(id) {
        $.ajax({
            url: domain + '/market/del',
            type: 'delete',
            dataType: 'json',
            data: {
                'id': id,
                'type': type
            },
            success: function (data) {
                if (data.code === 0) {
                    location.reload();
                } else {
                    layer.msg(data.msg);
                }
            }
        })
    }

    //发布
    function publish() {
        layer.open({
            type: 2
            , title: '新增'
            , area: ['850px', '580px']
            , shade: 0.5
            , maxmin: false
            , content: domain + '/page/toAddBuy'
        });
    }
</script>
</html>